package com.authManager.server.controller;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.authManager.entity.SysMenuUrl;
import com.authManager.server.service.ISysMenuUrlService;
import com.commons.base.model.AdminPage;
import com.commons.base.model.ResultCode;
import com.commons.base.model.ResultData;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * 【按钮 和 url关联表】 前端控制器
 * </p>
 *
 * @author 小旋风
 * @since 2018-8-20
 */

@Api(value = "/admin/sysMenuUrl", tags = "【按钮 和 url关联表】操作")
@RestController
@Validated
@RequestMapping(value = "/admin/sysMenuUrl", produces = BaseController.defProduces)
public class SysMenuUrlController extends BaseController {
	@SuppressWarnings("unused")
	private static final Logger log = LoggerFactory.getLogger(SysMenuUrlController.class);
	/*
	 * 【1】获取单个对象的方法用 get 做前缀。 【2】获取多个对象的方法用 list 做前缀。 【3】获取统计值的方法用 count 做nz前缀。
	 * 【4】插入的方法用 save（ 推荐 ） 或 insert 做前缀。 【5】删除的方法用 remove（ 推荐 ） 或 delete 做前缀。
	 * 【6】修改的方法用 update 做前缀。
	 */

	@Autowired
	ISysMenuUrlService sysMenuUrlService;

	@ApiOperation(value = "添加【按钮 和 url关联表】")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "query", dataType = "Long", name = "sysMenuId", value = "按钮id", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "requestUrl", value = "访问url", required = false), })
	@RequestMapping(value = "saveSysMenuUrl", method = { RequestMethod.POST })
	public ResultData<Object> saveSysMenuUrl(@RequestParam(required = false) Long sysMenuId,
			@RequestParam(required = false) String requestUrl) {
		SysMenuUrl sysMenuUrl = new SysMenuUrl();
		if (sysMenuId != null) {
			sysMenuUrl.setSysMenuId(sysMenuId);
		}
		if (StringUtils.isNotBlank(requestUrl)) {
			sysMenuUrl.setRequestUrl(requestUrl);
		}
		sysMenuUrlService.insert(sysMenuUrl);
		Object sysMenuUrlId = sysMenuUrl.getSysMenuUrlId();
		return new ResultData.Builder<Object>(ResultCode.success).setData(sysMenuUrlId).build();
	}

	@ApiOperation(value = "修改【按钮 和 url关联表】")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "query", dataType = "Long", name = "sysMenuUrlIdStr", value = "主键", required = true),
			@ApiImplicitParam(paramType = "query", dataType = "Long", name = "sysMenuId", value = "按钮id", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "requestUrl", value = "访问url", required = false), })
	@RequestMapping(value = "editSysMenuUrl", method = { RequestMethod.POST })
	public ResultData<Object> editSysMenuUrl(@RequestParam(required = true) Long sysMenuUrlId,
			@RequestParam(required = false) Long sysMenuId, @RequestParam(required = false) String requestUrl) {
		SysMenuUrl sysMenuUrl = sysMenuUrlService.selectById(sysMenuUrlId);
		if (sysMenuUrl == null) {
			return new ResultData.Builder<Object>(ResultCode.isNull).build();
		}
		sysMenuUrl = new SysMenuUrl();
		if (sysMenuUrlId != null) {
			sysMenuUrl.setSysMenuUrlId(sysMenuUrlId);
		}
		if (sysMenuId != null) {
			sysMenuUrl.setSysMenuId(sysMenuId);
		}
		if (StringUtils.isNotBlank(requestUrl)) {
			sysMenuUrl.setRequestUrl(requestUrl);
		}
		sysMenuUrlService.updateById(sysMenuUrl);
		return new ResultData.Builder<Object>(ResultCode.success).build();
	}

	@ApiOperation(value = "删除【按钮 和 url关联表】")
	@RequestMapping(value = "delSysMenuUrl", method = { RequestMethod.POST })
	public ResultData<Object> delSysMenuUrl(@RequestParam(required = true) Long sysMenuUrlId) {
		SysMenuUrl sysMenuUrl = sysMenuUrlService.selectById(sysMenuUrlId);
		if (sysMenuUrl == null) {
			return new ResultData.Builder<Object>(ResultCode.isNull).build();
		}
		// 自己根据需求放开注释吧
		// 物理删除
		// sysMenuUrlService.deleteById(sysMenuUrlId);
		// 逻辑删除
		// sysMenuUrl.setStatus(0);
		// sysMenuUrlService.updateById(sysMenuUrlId);
		return new ResultData.Builder<Object>(ResultCode.success).build();
	}

	@ApiOperation(value = "获取【按钮 和 url关联表】")
	@RequestMapping(value = "getSysMenuUrl", method = { RequestMethod.POST })
	public ResultData<SysMenuUrl> getSysMenuUrl(@RequestParam(required = true) Long sysMenuUrlId) {
		SysMenuUrl sysMenuUrl = sysMenuUrlService.selectById(sysMenuUrlId);
		if (sysMenuUrl == null) {
			return new ResultData.Builder<SysMenuUrl>(ResultCode.isNull).build();
		}
		return new ResultData.Builder<SysMenuUrl>(ResultCode.success).setData(sysMenuUrl).build();
	}

	@ApiOperation(value = "列表【按钮 和 url关联表】")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "query", dataType = "Integer", name = "pageNum", value = "页码", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "Integer", name = "pageSize", value = "每页大小", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "Long", name = "sysMenuUrlId", value = "主键", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "Long", name = "sysMenuId", value = "按钮id", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "requestUrl", value = "访问url", required = false), })
	@RequestMapping(value = "listSysMenuUrl", method = { RequestMethod.POST })
	public ResultData<AdminPage<SysMenuUrl>> listSysMenuUrl(
			@RequestParam(defaultValue = "1", required = false) Integer pageNum,
			@RequestParam(defaultValue = "10", required = false) Integer pageSize,
			@RequestParam(required = false) Long sysMenuUrlId, @RequestParam(required = false) Long sysMenuId,
			@RequestParam(required = false) String requestUrl) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("pageNum", pageNum);
		paramMap.put("pageSize", pageSize);
		// 主键
		if (sysMenuUrlId != null) {
			paramMap.put("sysMenuUrlId", sysMenuUrlId);
		}
		// 按钮id
		if (sysMenuId != null) {
			paramMap.put("sysMenuId", sysMenuId);
		}
		// 访问url
		if (StringUtils.isNotBlank(requestUrl)) {
			paramMap.put("requestUrl", requestUrl);
		}
		return sysMenuUrlService.listSysMenuUrl(paramMap);
	}
}
